package com.codename1.samples;

import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.Display;
import com.codename1.ui.Font;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.spinner.Picker;
import java.util.ArrayList;
import java.util.List;
import static com.codename1.ui.ComponentSelector.$;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class VerticalAlignTTFFontTest2798 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    private class FontWrapper {
        String name;
        Font font;
        
        FontWrapper(String name, Font font) {
            this.name = name;
            this.font = font;
        }
    }
    
    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        
        
        Form hi = new Form();
        
        hi.setToolbar(new Toolbar());
        
        hi.setTitle("Test Fonts");
        
        List<FontWrapper> fonts = new ArrayList<>();
        
        int fontSize = Display.getInstance().convertToPixels(3);
        fonts.add(new FontWrapper("Suisse Intl", Font.createTrueTypeFont("Suisse Int'l", "Suisse Int'l.ttf").derive(fontSize, Font.STYLE_PLAIN)));
        fonts.add(new FontWrapper("OpenSans-Regular", Font.createTrueTypeFont("OpenSans-Regular", "OpenSans-Regular.ttf").derive(fontSize, Font.STYLE_PLAIN)));
        fonts.add(new FontWrapper("OpenSans-Light", Font.createTrueTypeFont("OpenSans-Light", "OpenSans-Light.ttf").derive(fontSize, Font.STYLE_PLAIN)));
        fonts.add(new FontWrapper("OpenSans-Semibold", Font.createTrueTypeFont("OpenSans-Semibold", "OpenSans-Semibold.ttf").derive(fontSize, Font.STYLE_PLAIN)));
        fonts.add(new FontWrapper("ArialBD", Font.createTrueTypeFont("arialbd", "arialbd.ttf").derive(fontSize, Font.STYLE_PLAIN)));
        fonts.add(new FontWrapper("System Font Medium", Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)));
        fonts.add(new FontWrapper("System Font Large", Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE)));
        fonts.add(new FontWrapper("native:MainRegular", Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR, Font.NATIVE_MAIN_REGULAR).derive(fontSize, Font.STYLE_PLAIN)));
        for (FontWrapper font : fonts) {
            Button b = new Button(font.name);
            b.addActionListener(e->{
                new ShaiForm(font.name, font.font).show();
            });
            hi.add(b);
        }
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

    public class ShaiForm extends Form {

        private Form previous;
        private Font appFont;

        public ShaiForm(String fontName, Font appFont) {
            setToolbar(new Toolbar());
            setTitle("Test Font "+fontName);
            Form prev = CN.getCurrentForm();
            if (prev != null) {
                setBackCommand("Back", null, e->{
                    prev.showBack();
                });
                getToolbar().addCommandToLeftBar("Back", null, e->{
                    prev.showBack();
                });
            }
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));

            
            FontImage fntImage = FontImage.createFixed("\uE161", FontImage.getMaterialDesignFont(), 0x0, 100, 100);

            Label labelCustomTTF = new Label("custom TTF with g and y");
            Style labelStyle = labelCustomTTF.getAllStyles();
            labelStyle.setFont(appFont);
            labelCustomTTF.setIcon(fntImage);

            Label labelNative = new Label("native Font with g and y");
            labelNative.setIcon(fntImage);
            
            Label justTextLabel = new Label("Just text with g and y");
            justTextLabel.getStyle().setFont(appFont);

            Picker stringPickerCustom = new Picker();
            Style pickerStyle = stringPickerCustom.getAllStyles();
            pickerStyle.setFont(appFont);
            stringPickerCustom.setType(Display.PICKER_TYPE_STRINGS);
            stringPickerCustom.setStrings("custom TTF Font");
            stringPickerCustom.setText("custom TTF Font");

            Picker stringPickerNative = new Picker();
            stringPickerNative.setType(Display.PICKER_TYPE_STRINGS);
            stringPickerNative.setStrings("native Font");
            stringPickerNative.setText("native Font");

            $(labelCustomTTF, labelNative, justTextLabel, stringPickerCustom, stringPickerNative).selectAllStyles()
                    .setPadding(0);
            add(labelCustomTTF);
            add(labelNative);
            add(justTextLabel);
            add(stringPickerCustom);
            add(stringPickerNative);

        }

        public void show() {
            previous = Display.getInstance().getCurrent();
            super.show();
        }

        public void goBack() {
            previous.showBack();
        }

    }

}
